//
//  SYNewHomeMessageView.m
//  SanYiTeaching
//
//  Created by 郑浩然 on 2020/6/26.
//  Copyright © 2020 SanYi. All rights reserved.
//

#import "SYNewHomeMessageView.h"
#import "SYBackTopView.h"
#import "SYNoLabel.h"
#import "SYRequestAPI+HomePage.h"
#import "SYNewHomeMessageTableViewCell.h"
#import "NoticeMessageList.h"
#import "XGMessageModelOfMe.h"
#import "ChengJiYiChangVC.h"
#import "SYKaoQinYiChangViewController.h"
#import "CuoTiLvVC.h"
#import "SYKaoQinYiChangViewController.h"
#import "SYHouSanMingViewController.h"
@interface SYNewHomeMessageView ()<UITableViewDelegate,UITableViewDataSource>
@property (strong, nonatomic) SYBackTopView *backView;//顶部viwe
@property (strong, nonatomic) UITableView *curTableView;
@property (strong, nonatomic) NSMutableArray *dataLisArr;//数据源
@property (strong, nonatomic) SYNoLabel *noLab;//没有数据的lab
/**
 成绩异常
 */
@property (nonatomic, strong) ChengJiYiChangVC *loginHomePageVC;

/**
 考勤异常
 */
@property (nonatomic, strong) SYKaoQinYiChangViewController *kqycVC;

/**
 错题率
 */
@property (nonatomic, strong) CuoTiLvVC *cuoTiLvView;

/**
 成绩后三名
 */
@property (nonatomic, strong) SYHouSanMingViewController *hsmVC;
@end


@implementation SYNewHomeMessageView


- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor colorWithHexString:@"#EEEEEE"];
        [self initSubView];
    }
    return self;
}
- (void)initSubView {
    [self addSubview:self.backView];
    [self.backView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.equalTo(self);
        make.height.mas_equalTo(50);
    }];
    
    [self addSubview:self.curTableView];
    [self.curTableView registerCellWithNIbNames:@[@"SYNewHomeMessageTableViewCell"]];
    [self.curTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.backView.mas_bottom).offset(0);
        make.left.right.bottom.equalTo(self);
    }];
    self.dataLisArr = [NSMutableArray array];
}

- (void)setClassId:(NSString *)classId {
    [self.dataLisArr removeAllObjects];
    [MBProgressHUD mj_showLoading];
    __weak typeof(self)weakSelf = self;
    [SYRequestAPI sy_postNoticeReceiveServiceGetNoticeInfoNewClassId:classId success:^(NSArray * _Nonnull dataListArr) {
        [MBProgressHUD mj_hideHUD];
        [weakSelf.dataLisArr addObjectsFromArray:dataListArr];
        [weakSelf.curTableView reloadData];
    } fail:^(NSError *error) {
        [MBProgressHUD mj_hideHUD];
        
    }];
    
}

#pragma mark - tabl
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataLisArr.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 100;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    SYNewHomeMessageTableViewCell *messageCell = [tableView dequeueReusableCellWithIdentifier:@"SYNewHomeMessageTableViewCell" forIndexPath:indexPath];
    messageCell.selectionStyle = UITableViewCellSelectionStyleNone;
    NSDictionary *dict = self.dataLisArr[indexPath.row];
    NSString *MsgContent = [dict nilObjectForKey:@"MsgContent"];
    NSDictionary *msgDict = dicWithJsonString(MsgContent);
    messageCell.titleLab.text =msgDict[@"body"];
    messageCell.messageTypeLab.text = [NSString stringWithFormat:@"消息类型:%@",[dict nilObjectForKey:@"MsgTypeName"]];
    messageCell.timeLab.text = [NSString stringWithFormat:@"时间:%@",[dict nilObjectForKey:@"CreateDate"]];
    messageCell.fudaoTypeLab.text = [NSString stringWithFormat:@"%@",[dict nilObjectForKey:@"tutorStatusName"]];
    return messageCell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSDictionary *dict = self.dataLisArr[indexPath.row];
    NSString *tutorStatus = [dict nilObjectForKey:@"tutorStatus"];
    if ([tutorStatus isEqualToString:@"0"]) { //未辅导
        NSString *Status = [dict nilObjectForKey:@"MsgType"];//1考勤异常 2排名异常  3错题率  4成绩后三名
        XGMessageModelOfMe *model = [[XGMessageModelOfMe alloc] init];
        model.msgId = [dict nilObjectForKey:@"ID"];
        if ([Status isEqualToString:@"1"]) {
                    //考勤异常
                    
                _kqycVC = [[SYKaoQinYiChangViewController alloc] init];
                _kqycVC.view.frame = CGRectMake(0, 0, APP_WIDTH, APP_HEIGHT);
                _kqycVC.xgModel = model;
                [_kqycVC requestDataListArr];
                [[UIApplication sharedApplication].keyWindow.rootViewController.view addSubview:_kqycVC.view];
            }else if ([Status isEqualToString:@"2"]){
                //排名异常
                _loginHomePageVC = [[ChengJiYiChangVC alloc] init];
                _loginHomePageVC.view.frame = CGRectMake(0, 0, APP_WIDTH, APP_HEIGHT);
                _loginHomePageVC.model = model;
                [_loginHomePageVC getPaiMingChengJiYiChangUp];
                [_loginHomePageVC getPaiMingChengJiYiChangDown];
                [[UIApplication sharedApplication].keyWindow.rootViewController.view addSubview:_loginHomePageVC.view];
            }else if ([Status isEqualToString:@"3"]){
                //错题率
                _cuoTiLvView = [[CuoTiLvVC alloc]init];
                _cuoTiLvView.msgId = model.msgId;
                [_cuoTiLvView getRequest];
                [[UIApplication sharedApplication].keyWindow.rootViewController.view addSubview:_cuoTiLvView.view];
                //        [XSInfoView showInfo:@"此功能正在开发中..." onView:[UIApplication sharedApplication].keyWindow.rootViewController];
            }else if ([Status isEqualToString:@"4"]){
                //成绩后三名
                _hsmVC = [[SYHouSanMingViewController alloc] init];
                _hsmVC.xgModel = model;
        //        [_hsmVC requestData];
                [[UIApplication sharedApplication].keyWindow.rootViewController.view addSubview:_hsmVC.view];
            }
    } else {
        NSString *tutorStatusName = [dict nilObjectForKey:@"tutorStatusName"];
        [MBProgressHUD zr_showError:tutorStatusName];
    }
    
}


#pragma mark - set
//头部
- (SYBackTopView *)backView {
    if (!_backView) {
        _backView = [[SYBackTopView alloc] initWithTitleName:@"班级消息" baseView:self];
    }
    return _backView;
}
//表格
- (UITableView *)curTableView {
    if (!_curTableView) {
        _curTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 10, 100) style:UITableViewStylePlain];
        _curTableView.delegate = self;
        _curTableView.dataSource = self;
        _curTableView.tableFooterView = [UIView new];
        _curTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        
    }
    return _curTableView;
}
#pragma 字符串转换成字典
NS_INLINE NSDictionary *dicWithJsonString(NSString *jsonString)
{
    if (jsonString == nil) {
        return nil;
    }
    NSData *jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
    NSError *err;
    NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&err];
    if(err) {
//        NSLog(@"json解析失败：%@",err);
        return nil;
    }
//    NSLog(@"得到消息列表 %@",dic);
    return dic;
}

@end
